Imports OMANE.Common.UI
Imports System.Collections.ObjectModel
Imports System.Windows
Imports Infragistics.Windows.Chart
Imports System.Windows.Controls
Imports System.ComponentModel
Imports Microsoft.Practices.Prism.Commands

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class VMDashboardDefinition
    Inherits VMSectionBase

    Private _LayoutDefinition As VMDashboardLayoutDefinition
    Private _Zones As New ObservableCollection(Of VMDashboardDefinitionZone)()

    Sub New(ByVal dbinfo As VMDashboardLayoutDefinition)
        ' TODO: Complete member initialization 
        _LayoutDefinition = dbinfo
    End Sub
    Sub New()

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property LayoutDefinition As VMDashboardLayoutDefinition
        Get
            Return _LayoutDefinition
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property Zones As ObservableCollection(Of VMDashboardDefinitionZone)
        Get
            Return _Zones
        End Get
    End Property

    Sub ReCalculate()
        For Each z In Zones
            z.ReCalculate()
        Next
    End Sub

    <Xml.Serialization.XmlIgnore()> _
    Public Property Columns As New Dictionary(Of Integer, ColumnDef)

    <Xml.Serialization.XmlIgnore()> _
    Public Property Rows As New Dictionary(Of Integer, RowDef)


    Friend wpfrows As IEnumerable(Of RowDefinition)
    Friend wpfcols As IEnumerable(Of ColumnDefinition)

    Private _ColumnsSerializable As ColumnDef()
    Public Property ColumnsSerializable As ColumnDef()
        Get
            _ColumnsSerializable = Columns.Values.Cast(Of ColumnDef).ToArray()
            Return _ColumnsSerializable
        End Get
        Set(ByVal value As ColumnDef())
            OnPropertyChanging("ColumnsSerializable")
            _ColumnsSerializable = value
            OnPropertyChanged("ColumnsSerializable")
        End Set
    End Property

    Private _RowsSerializable As RowDef()
    Public Property RowsSerializable As RowDef()
        Get
            _RowsSerializable = Rows.Values.Cast(Of RowDef).ToArray()
            Return _RowsSerializable
        End Get
        Set(ByVal value As RowDef())
            OnPropertyChanging("RowsSerializable")
            _RowsSerializable = value
            OnPropertyChanged("RowsSerializable")
        End Set
    End Property

    Public Function ToXElement() As XElement
        Dim grd As Grid = Nothing
        Dim grdbool = EditingGrid.Associations.TryGetValue(Me, grd)
        Dim rs = Nothing
        Dim cs = Nothing
        If grdbool Then
            If grd.RowDefinitions.Count > 0 Then
                rs = From r In grd.RowDefinitions
                     Where grd.RowDefinitions.IndexOf(r) Mod 2 = 0
                     Select <Row Size=<%= r.Height.Value %> Type=<%= r.Height.GridUnitType %>/>
            End If

            If grd.ColumnDefinitions.Count > 0 Then
                cs = From r In grd.ColumnDefinitions
                     Where grd.ColumnDefinitions.IndexOf(r) Mod 2 = 0
                     Select <Column Size=<%= r.Width.Value %> Type=<%= r.Width.GridUnitType %>/>
            End If
        End If

        Dim x = <DashboardDefinition Name=<%= LayoutDefinition.Name %> RowCount=<%= LayoutDefinition.RowCount %> ColumnCount=<%= LayoutDefinition.ColumnCount %>>
                    <LayoutDefinition>
                        <%= rs %>
                        <%= cs %>
                    </LayoutDefinition>


                    <Zones>
                        <%= From elt In Zones Select elt.ToXElement(EditingGrid.Associations(Me)) %>
                    </Zones>
                </DashboardDefinition>

        Return x
    End Function

    Sub FromXElement(ByVal x As XElement)
        'Throw New NotImplementedException

        Dim elts = x.<Zones>.<DashboardDefinitionZone>

        For Each elt In elts
            Dim z = New VMDashboardDefinitionZone()
            z.FromXElement(elt)
            Zones.Add(z)
        Next
    End Sub

End Class

<Serializable()> _
Public Class GridDef
    Inherits ViewModelBase



    Private _Size As GridLength = New GridLength(1.0, GridUnitType.Star)

    <Xml.Serialization.XmlIgnore()> _
    Public Property Size As GridLength
        Get
            Return _Size
        End Get
        Set(ByVal value As GridLength)
            OnPropertyChanging("Size")
            _Size = value
            OnPropertyChanged("Size")
        End Set
    End Property

    Public Property SizeSerializable As String
        Get
            Return _Size.ToString()
        End Get
        Set(ByVal value As String)
            If value.Trim().EndsWith("*") Then
                If value.Trim() = "*" Then
                    Size = New GridLength()
                Else
                    Dim n = Convert.ToDouble(value.Trim().Trim("*").Trim(), Globalization.CultureInfo.InvariantCulture)
                    Size = New GridLength(n, GridUnitType.Star)
                End If
            Else
                Size = New GridLength(Convert.ToDouble(value.Trim(), Globalization.CultureInfo.InvariantCulture), GridUnitType.Pixel)
            End If
        End Set
    End Property
End Class

<Serializable()> _
Public Class ColumnDef
    Inherits GridDef
End Class

<Serializable()> _
Public Class RowDef
    Inherits GridDef
End Class
